/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.ppsa.Servlet;

import com.ppsa.bean.UsuarioBean;
import com.ppsa.common.Constantes;
import com.ppsa.common.Util;
import com.ppsa.logic.ConsultaLogic;
import com.ppsa.logic.MantenimientoLogic;
import java.io.IOException;
import java.util.HashMap;
import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;
import org.apache.commons.lang.ObjectUtils;
import org.apache.log4j.Logger;

/**
 *
 * @author AdminTdp
 */
public class ServletConsulta extends HttpServlet {
    
    static final Logger m_log = Logger.getLogger(ServletConsulta.class.getName());
    /**
     * Processes requests for both HTTP
     * <code>GET</code> and
     * <code>POST</code> methods.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        m_log.info("[processRequest] << ENTER");
        response.setContentType("text/html;charset=UTF-8");
        //PrintWriter out = response.getWriter();
        String strpv_dispatcher = "";
        HttpSession objL_sesion = objL_sesion = request.getSession(true);
        UsuarioBean objL_usuario = new UsuarioBean();
        ConsultaLogic objL_consultaLogic = null;
        MantenimientoLogic objL_mantenimientoLogic = null;        

        try {

            String strpv_metodo = Util.getParameterString(request, "metodo", null , "");
            m_log.info("metodo := " + strpv_metodo);
            if (strpv_metodo.equals("login")) {
                
                String strpv_nombreUsuario = ObjectUtils.toString(request.getParameter("username"));
                String strpv_clave = ObjectUtils.toString(request.getParameter("password"));
                //VALIDACION DEL USUARIO
                objL_mantenimientoLogic = new MantenimientoLogic();
                HashMap<String,String> hmL_valida = objL_mantenimientoLogic.validarUsuario(strpv_nombreUsuario, strpv_clave);
                if( hmL_valida.get("strpv_mensaje").equals("OK") ) {
                    objL_usuario.setStrpv_codigoUsuario(hmL_valida.get("strpv_codigoUsuario"));
                    objL_usuario.setStrpv_nombreUsuario(hmL_valida.get("strpv_nombreUsuario"));
                    objL_usuario.setStrpv_codigoPerfil(hmL_valida.get("strpv_codigoPerfil"));
                    
                    objL_consultaLogic = new ConsultaLogic();
                    objL_usuario.setObjpv_perfilBean(objL_consultaLogic.obtenerPerfil(hmL_valida.get("strpv_codigoPerfil")));
                    objL_usuario.setLstpv_menuPrincipal( Util.obtenerMenuPrincipal(objL_usuario.getObjpv_perfilBean()) );
                    objL_sesion.setAttribute("objL_usuario", objL_usuario);
                    request.setAttribute("strpv_countAlert", objL_consultaLogic.obtenerTotalAlerta(objL_usuario)  );
                    strpv_dispatcher = Constantes.PAGINA_MAIN;
                } else {
                    request.setAttribute("mensaje_error", hmL_valida.get("strpv_mensaje"));
                    strpv_dispatcher = Constantes.PAGINA_LOGIN;
                }
                RequestDispatcher dispatcher = request.getRequestDispatcher(strpv_dispatcher);
                dispatcher.forward(request, response);
                return;
                
            }else if(strpv_metodo.equals("pagina")) {
                objL_consultaLogic = new ConsultaLogic();
                objL_mantenimientoLogic = new MantenimientoLogic();
                UsuarioBean  objL_usuarioBeanSession = new UsuarioBean();
                
                String strpv_pagina = Util.getParameterString(request, "a", null , "");
                String strpv_codigo = Util.getParameterString(request, "i", null, "");
                String strpv_accion = Util.getParameterString(request, "accion", null, "");
                
                m_log.info("strpv_pagina ( a ) := " + strpv_pagina );
                m_log.info("strpv_codigo ( i ) := " + strpv_codigo );
                m_log.info("strpv_accion ( accion ) := " + strpv_accion);

                strpv_dispatcher = Util.obtenerEquivalenciaPagina( strpv_pagina );                
                objL_usuarioBeanSession = (UsuarioBean) objL_sesion.getAttribute("objL_usuario");

                request.setAttribute("strpv_titulo", Util.obtenerTituloPagina(strpv_pagina));
                request.setAttribute("strpv_subTitulo", strpv_accion);
                request.setAttribute("lst_menuTop", Util.obtenerMenuPagina(strpv_pagina , objL_usuarioBeanSession.getObjpv_perfilBean() ));
                request.setAttribute("strpv_countAlert", objL_consultaLogic.obtenerTotalAlerta(objL_usuarioBeanSession)  );
                request = (HttpServletRequest) objL_mantenimientoLogic.cargarDatos(request, strpv_pagina, strpv_accion, strpv_codigo, objL_usuarioBeanSession);

            }else if( strpv_metodo.equals("cerrarSession") ){
                strpv_dispatcher = Constantes.PAGINA_LOGIN ;
                objL_sesion.removeAttribute("objL_usuario");
            }

            if (objL_sesion.getAttribute("objL_usuario") == null){        	
                RequestDispatcher dispatcher = request.getRequestDispatcher(  Constantes.PAGINA_LOGIN );
            }

            m_log.info("dispatcher -> " + strpv_dispatcher);
            RequestDispatcher dispatcher = request.getRequestDispatcher( strpv_dispatcher );
            dispatcher.forward(request, response);
            m_log.info("[processRequest] >> EXIT");
        } catch ( Exception ex ) {
            ex.printStackTrace();
        } finally {
            //out.close();
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /**
     * Handles the HTTP
     * <code>GET</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        
        processRequest(request, response);
    }

    /**
     * Handles the HTTP
     * <code>POST</code> method.
     *
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
            throws ServletException, IOException {
        processRequest(request, response);
    }

    /**
     * Returns a short description of the servlet.
     *
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}
